# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>

_realname=nodejs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.10.43
pkgrel=1
pkgdesc="Evented I/O for V8 javascript (mingw-w64)"
arch=('any')
url="https://nodejs.org/"
license=("MIT")
makedepends=("python2" "make" "gyp")
# If can switch to shared dependencies instead of
# static, bundled then here is the full list. For
# for now, reluctantly the only shared dependency
# is OpenSSL.
# depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
#          "${MINGW_PACKAGE_PREFIX}-http-parser"
#          "${MINGW_PACKAGE_PREFIX}-v8"
#          "${MINGW_PACKAGE_PREFIX}-libuv"
#          "${MINGW_PACKAGE_PREFIX}-openssl"
#          "${MINGW_PACKAGE_PREFIX}-zlib")
depends=("${MINGW_PACKAGE_PREFIX}-openssl" "winpty")
#options=('!emptydirs' '!strip') # 'debug')

source=("https://nodejs.org/dist/v${pkgver}/node-v${pkgver}.tar.gz"
        "0001-Win32-Update-NODE_NET_SOCKET_-READ-WRITE-macros.patch"
        "0002-Win32-Update-NODE_COUNT_GC_PERCENTTIME-macro.patch"
        "0003-Win32-etw-Use-INT_PTR-instead-of-INT32-in-pointer-ca.patch"
        "0004-Win32-Determine-and-use-EXEEXT.patch"
        "0005-Win32-gyp-If-sys.platform-is-msys-use-make-not-msvs.patch"
        "0006-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch"
        "0007-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch"
        "0008-Add-output-dir-option-to-configure-node_gyp-py.patch"
        "0009-Define-_WIN32_WINNT_0x0600-when-node_use_perfctr.patch"

        "0101-Fixing-for-python2-name.patch"
        "0102-Mat_Sutcliffe-node_mingw64-without-MSYS-path-conversion.patch"
        "0103-Mat_Sutcliffe-node_mingw64-MSYS-path-conversion.patch"
        "0104-Use-system-gyp.patch"
        "0105-Add-gyp-debug-option-to-configure.patch"
        "0106-MinGW-w64-add-platform-wintoolset-as-msvc-or-mingw-w64-for-node-gyp.patch"
        "0107-cygpath-conversion-in-npm-shell-wrapper.patch"

        "0200-v8-3.26.33-Disable-USING_V8_SHARED-for-v8_snapshot-static-lib.patch"
        "0201-v8-3.26.33-Include-win32-headers-h-then-undef-MemoryBarrier.patch"
        "0203-v8-3.26.33-Use-soname_version.dll-not-so.soname_version-on-Windows.patch"
        "0300-uv-Use-4-arg-swprintf-on-MinGW-w64.patch"
        "0301-uv-Set-WIN32_WINNT_to_0x501_for_XP.patch"
        'node-gyp'
        'node-gyp.cmd')

sha256sums=('c672452a61dd37cf2779bc158b65a5a22af343da19fec1cddf9bced382a2595a'
            '9e821cb1d7678ea80edb054e78c4df2327b4c049071deaf1ff7c50bbbe38c522'
            '39e3096b71d7ec34d76f8611599fb7ca36ce4a43719d79706581d23949e49684'
            'f087fd45b15e1232b84801ae057b72e96c795287217b5eb4701b5301d05716fa'
            'e7c0af554f51ccc825efb73dc6bafb138edd72d950ef2edcb840f7b2a6ad70d0'
            'f61be6e6be499f30f5382597c399cb5f63a1eb062d24d8dc8607d6d43982c028'
            '5640fb5b0d51af59295f50a92e3d78db3ad2a4f1ff3dd992d1ec01ab2132afd4'
            'e544c9d2f557d58c3acc9d70ffc7ffb512b0c5432ff296a53f2fffe9bf814787'
            'e5d44805f05d5bdd4ddaa53421f91a728e011100937324e14e6956d3d32e0d26'
            '6fe9662ba50eff1b10057ab6083073e2a82d11e4f23abb74db3810e5039317a8'
            'd66a3d5c261eaf00f87f375a06d2877cc99bea74a191fdb0c1a3de7ed599fdc2'
            'efccb08c12227e9f201c310398c426a3a867554e7c78ccc6d86c9296b81508cc'
            'db690923152f6302d1305a1e7776b7bd07004693529520dc4e45e60d6b5e5162'
            'da4d21c363fbb5f2ae4bf8d948024a37b143f453885fb262c013d87b677510c9'
            'b741a1f93f0a488ececa09c54c03678072752c9f1cfdbb18f6460eb0f49ee857'
            '5a43d4ef2bba3029171578f774fa26891cdd7a9b09193d2bd88e45ef42746fac'
            '623a219e683c96dd2419a7f0350908d2a55e7c1c96bb37fd25832fb269a1ae9f'
            'f300370948f3bf6dd0c5396fab8b970329c86f5f9153dd36a91f4831fcec0ce3'
            '501956054354b03d5920ab9e8fff9e57ca467c338e20de0a5d70a8799b7a4e5b'
            '75031ee8ab6e5a2fac5b3eda498cb81abf5db9a41d76d2b1b5aba2508facd06c'
            'fe2b71c01403e965a0d84bbd0c008e312bfe0738d70878dadc90199dc3227657'
            '09b87c871aa8cab0e686ef62b6268cbeaf8346901cf0b91df1b8c145dae522b1'
            '8f00d54bc60c62cfc6c63657fca0a522ee485ab2f30de721bd09a0f868e81257'
            '55294eca72a62b2730470a4586b5caab2ec3a43d5412566770541c9fc59e4135')

prepare() {
  cd ${srcdir}/node-v${pkgver}

  git init

  # Using perfctrs makes Vista the minimum OS. MinGW-w64's perflib.h is missing lots
  # of stuff too, so the patches aren't enough.
  patch -p1 -i "${srcdir}"/0001-Win32-Update-NODE_NET_SOCKET_-READ-WRITE-macros.patch
  patch -p1 -i "${srcdir}"/0002-Win32-Update-NODE_COUNT_GC_PERCENTTIME-macro.patch
  patch -p1 -i "${srcdir}"/0003-Win32-etw-Use-INT_PTR-instead-of-INT32-in-pointer-ca.patch
  patch -p1 -i "${srcdir}"/0004-Win32-Determine-and-use-EXEEXT.patch
  patch -p1 -i "${srcdir}"/0005-Win32-gyp-If-sys.platform-is-msys-use-make-not-msvs.patch
  patch -p1 -i "${srcdir}"/0006-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch

  patch -p1 -i "${srcdir}"/0007-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch
  patch -p1 -i "${srcdir}"/0008-Add-output-dir-option-to-configure-node_gyp-py.patch
  patch -p1 -i "${srcdir}"/0009-Define-_WIN32_WINNT_0x0600-when-node_use_perfctr.patch

  patch -p1 -i "${srcdir}"/0101-Fixing-for-python2-name.patch
  patch -p1 -i "${srcdir}"/0102-Mat_Sutcliffe-node_mingw64-without-MSYS-path-conversion.patch
# We don't want this patch, someone might though. May need fixing for MSYS2 anyway.
#  patch -p1 -i "${srcdir}"/0103-Mat_Sutcliffe-node_mingw64-MSYS-path-conversion.patch
  patch -p1 -i "${srcdir}"/0104-Use-system-gyp.patch
  patch -p1 -i "${srcdir}"/0105-Add-gyp-debug-option-to-configure.patch
  patch -p1 -i "${srcdir}"/0106-MinGW-w64-add-platform-wintoolset-as-msvc-or-mingw-w64-for-node-gyp.patch
  patch -p1 -i "${srcdir}"/0107-cygpath-conversion-in-npm-shell-wrapper.patch

  pushd deps/v8 > /dev/null
  patch -p1 -i "${srcdir}"/0200-v8-3.26.33-Disable-USING_V8_SHARED-for-v8_snapshot-static-lib.patch
  patch -p1 -i "${srcdir}"/0201-v8-3.26.33-Include-win32-headers-h-then-undef-MemoryBarrier.patch
  patch -p1 -i "${srcdir}"/0203-v8-3.26.33-Use-soname_version.dll-not-so.soname_version-on-Windows.patch
  popd > /dev/null

  pushd deps/uv > /dev/null
  patch -p1 -i "${srcdir}"/0300-uv-Use-4-arg-swprintf-on-MinGW-w64.patch
  patch -p1 -i "${srcdir}"/0301-uv-Set-WIN32_WINNT_to_0x501_for_XP.patch
  popd > /dev/null
}

build() {

  local -a extra_config

  cd ${srcdir}
  sleep 5

  [[ -d ${CARCH} ]] && rm -rf ${CARCH}
  mv node-v${pkgver} ${CARCH}
  pushd ${CARCH}

# node.js uses a patched version of v8 3.26.33. If/when they upgrade
# we might be able to use this:
#    --shared-v8 \
#    --shared-v8-includes=$(pkgconf --variable=includedir v8) \
#    --shared-v8-libpath=$(pkgconf --variable=libdir v8) \
#    --shared-v8-libname=v8.$(pkgconf --modversion v8) \

#    --shared-cares-libpath=${MINGW_PREFIX}/lib \
#    --shared-cares-includes=${MINGW_PREFIX}/include \

#    --shared-cares \
#    --shared-libuv \
#    --shared-zlib \
#    --shared-http-parser \

# --output-dir= and 0004-Add-output-dir-option-to-configure-node_gyp-py.patch
# attempts to allow using the bundled (hence static) OpenSSL. The problem is
# that the link command line ends up being ~76k characters. This patch helps
# but it gets us down to ~45k. To further reduce this, the build folders of
# "Release/out.target" would be the next target.
#  OUTDIR=$(cd ../../..; echo $PWD)
#      --output-dir=/e/m2/t \
#
# --without-perfctr because MinGW-w64's perflib.h is missing a needed define
# (PERF_COUNTERSET_MULTI_AGGREGATE) and the win32 perfcnt stuff uses __out,
# __in, and __in_opt arg decorators.

  export CC=gcc
  export CXX=g++
  export LINK=g++

  if check_option "debug" "y"; then
    extra_config+=( --debug )
  fi

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --dest-os=win \
    --shared-openssl \
    --shared-openssl-includes=${MINGW_PREFIX}/include \
    --shared-openssl-libpath=${MINGW_PREFIX}/lib \
    --without-perfctr \
    --gyp-debug=all \
    "${extra_config[@]}"

  make
}

package() {
  cd "${srcdir}/${CARCH}"
  make DESTDIR="${pkgdir}" install

  install -d "${pkgdir}"/${MINGW_PREFIX}/share/doc/nodejs
  cp -r doc/api/{*.html,assets} \
    "${pkgdir}"/${MINGW_PREFIX}/share/doc/nodejs

  install -D -m644 LICENSE \
    "${pkgdir}"/${MINGW_PREFIX}/share/licenses/nodejs/LICENSE

  install -m755 "${srcdir}/node-gyp" "${pkgdir}"${MINGW_PREFIX}/bin/node-gyp
  install -m755 "${srcdir}/node-gyp.cmd" "${pkgdir}"${MINGW_PREFIX}/bin/node-gyp.cmd
  install -m755 "${pkgdir}${MINGW_PREFIX}/lib/node_modules/npm/bin/npm.cmd" "${pkgdir}${MINGW_PREFIX}/bin/npm.cmd"

  # Use winpty to wrap the exe when executed from bash. Please don't move this into a patch as hopefully one day we won't need this hack.
  local _exename="node"
  mv "${pkgdir}"${MINGW_PREFIX}/bin/${_exename}.exe "${pkgdir}"${MINGW_PREFIX}/bin/${_exename}_exe
  echo "#!/usr/bin/env bash" > "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"
  echo '/usr/bin/winpty "$( dirname ${BASH_SOURCE[0]} )/'${_exename}'.exe" "$@"' >> "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"
  mv "${pkgdir}"${MINGW_PREFIX}/bin/${_exename}_exe "${pkgdir}"${MINGW_PREFIX}/bin/${_exename}.exe

  # copy missing manpages
  cp -r "${pkgdir}${MINGW_PREFIX}/lib/node_modules/npm/man" "${pkgdir}${MINGW_PREFIX}/share"

  # /mingw64/lib/node_modules/npm/lib/npm.js is ridden with tests for win32, so that should probably be changed someday.
}
